package minijava;

import java.util.LinkedList;
import java.util.Hashtable;
import java.util.Vector;
import java.lang.Integer;
import minijava.Graph;

public class LabeledGraph<T> extends Graph{

    private Hashtable<T, Integer> labels;
    private Vector<T> ids;

    public LabeledGraph(){
	super();
	labels = new Hashtable<T, Integer>();
	ids = new Vector<T>();
    }

    public void addVertex(T l){
	if(labels.containsKey(l)){
	    System.out.println("LabeledGraph.addVertex: label '" + l.toString() + "' has already been used");
	    return;
	}
	labels.put(l, vertices.size());
	ids.add(l);
	vertices.add(new LinkedList<Integer>());
    }

    public void addEdge(T s, T d){
	Integer source = labels.get(s);
	if(source == null){
	    System.out.println("LabeledGraph.addEdge: label '" + s.toString() + "' does not exist");
	    return;
	}
	LinkedList<Integer> edges = vertices.elementAt(source);
	Integer dest = labels.get(d);
	if(dest == null){
	    System.out.println("LabeledGraph.addEdge: label '" + d.toString() + "' does not exist");
	    return;
	}
	edges.add(new Integer(dest));
    }

    public LinkedList<T> findLabeledCycle(){
	LinkedList<Integer> c = findCycle();
	if(c == null)
	    return null;
	LinkedList<T> lc = new LinkedList<T>();
	for(Integer i : c){
	    lc.add(ids.elementAt(i));
	}
	return lc;
    }
}
